from apps.common.base.dao import BaseDao
from apps.pay.recordIncomes.serializers import *
from apps.user.dao import UserDao
from apps.system.lessee.dao import LesseeDao
from decimal import Decimal

class RecordIncomesDao(BaseDao):
    MODEL_CLASS = RecordIncomes
    SERI_CLASS = RecordIncomesBaseSerializer

    def create(self, data):
        if data.get('money'):
            data['money'] = Decimal(data.get('money')).quantize(Decimal('0.00'))
        if data.get('moneyLocked'):
            data['moneyLocked'] = Decimal(data.get('moneyLocked')).quantize(Decimal('0.00'))
        if data.get('balanceIncomes'):
            data['balanceIncomes'] = Decimal(data.get('balanceIncomes')).quantize(Decimal('0.00'))
        if data.get('serviceCharge'):
            data['serviceCharge'] = Decimal(data.get('serviceCharge')).quantize(Decimal('0.00'))
        if data.get('realMoney'):
            data['realMoney'] = Decimal(data.get('realMoney')).quantize(Decimal('0.00'))
        serializers = RecordIncomesBaseSerializer(data=data)
        if serializers.is_valid():
            serializers.save()

            # 同步当前积分
            if data.get('user'):
                _user = UserDao().findOneByObjId(data.get('user'))
                _objId = serializers.data.get('id')
                if serializers.data.get('payway') == 'incomes':
                    self.update(_objId, {'balanceIncomes': _user.incomes})
                elif serializers.data.get('payway') == 'balance':
                    self.update(_objId, {'balanceIncomes': _user.balance})
                elif serializers.data.get('payway') == 'incomesLocked':
                    self.update(_objId, {'balanceIncomes': _user.incomesLocked})
                elif serializers.data.get('payway') == 'balanceLocked':
                    self.update(_objId, {'balanceIncomes': _user.balanceLocked})
                elif serializers.data.get('payway') == 'contributionValue':
                    self.update(_objId, {'balanceIncomes': _user.contributionValue})
                elif serializers.data.get('payway') == 'incomesCredit':
                    self.update(_objId, {'balanceIncomes': _user.incomesCredit})
                elif serializers.data.get('payway') == 'incomesOriginal':
                    self.update(_objId, {'balanceIncomes': _user.incomesOriginal})
                elif serializers.data.get('payway') == 'incomesReduction':
                    self.update(_objId, {'balanceIncomes': _user.incomesReduction})
                elif serializers.data.get('payway') == 'cashSellingMoney':
                    self.update(_objId, {'balanceIncomes': _user.cashSellingMoney})
                elif serializers.data.get('payway') == 'shop_balance':
                    self.update(_objId, {'balanceIncomes': _user.shop_balance})
            else:
                _lessee = LesseeDao().getLessee()
                _objId = serializers.data.get('id')
                if serializers.data.get('payway') == 'freePoolDay':
                    self.update(_objId, {'balanceIncomes': _lessee.freePoolDay})
                elif serializers.data.get('payway') == 'freePoolMonth':
                    self.update(_objId, {'balanceIncomes': _lessee.freePoolMonth})
                elif serializers.data.get('payway') == 'supplyAwards':
                    self.update(_objId, {'balanceIncomes': _lessee.supplyAwards})
                elif serializers.data.get('payway') == 'helpFundPool':
                    self.update(_objId, {'balanceIncomes': _lessee.helpFundPool})
                elif serializers.data.get('payway') == 'grossProfit':
                    self.update(_objId, {'balanceIncomes': _lessee.grossProfit})
                elif serializers.data.get('payway') == 'operationAwardPool':
                    self.update(_objId, {'balanceIncomes': _lessee.operationAwardPool})

        else:
            print('create --error--', self.MODEL_CLASS, serializers.errors)
            return serializers.errors